package com.hardy.Thread;

import java.util.Stack;

/**
 * Created by Administrator on 2018/2/27.
 */
public class BlockQueueTest<T> {
    private final static Object pushLock = new Object();
    private final static Object popLock = new Object();
    private Stack<T> stack;
    public BlockQueueTest(){
        stack = new Stack<T>();
    }

    public synchronized void push(T t){
        int MAX_SIZE = 3;
        if(stack.size()>MAX_SIZE){
            pushLock();
        }
        stack.push(t);
        popUnLock();
    }
    public T pop(){
        if(stack.size()==0){
            popLock();
        }
        T t= stack.pop();
        pushUnlock();
        return t;
    }
    private void pushLock(){
        synchronized (pushLock){
            try {
                pushLock.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void pushUnlock(){
        synchronized (pushLock){
            pushLock.notify();
        }
    }

    private void popLock(){
        synchronized (popLock){
            try {
                popLock.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    private void popUnLock(){
        synchronized (popLock){
            popLock.notify();
        }
    }
}
